/*
   (C) 2005 - Michael Ocean - Boston University

  This work is a part of the larger snBench project.  Information
  can be found at: http://csr.bu.edu/snbench

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package sxe;


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;

import snobject.snObject;

import http.Client;
import http.Status;

public class Poster
{
	private static java.net.URI sxeURI;
	private static BufferedReader br;
	private static boolean bRunning;
	
	static public void usage() {
		System.err.println(
			"Usage: uri \n" +
			"  uri - SXE address, \"http://hostname:port\"\n");
	}

	static public File getfilename(){
		File f = new File("./testsuite/");
		File fAr[] = f.listFiles();
		for(int i=0;i<fAr.length;i++){
			System.out.println("[" + i + "]\t" + fAr[i]);
		}
		System.out.print("Specify a file number");
		String sNum = getcmd();
		System.out.println("Got [" + sNum + "]");
		
		int j = Integer.parseInt(sNum);
		return fAr[j];
	}
	
	static public void do_stuff(){
		bRunning = true;
		
		while(bRunning){
			showcmds();
			String sCmd = getcmd();
			System.out.println("Got [" + sCmd + "]");
			
			if(sCmd.equalsIgnoreCase("quit")){
				bRunning=false;
			}
			else if(sCmd.equalsIgnoreCase("post")){
				poster();
			}
			else if(sCmd.equalsIgnoreCase("delete")){
				deleter();
			}
		}
		
	}
	private static String getcmd() {
		System.out.print(":");
		
		try{
			return br.readLine().replace("\n","").replace("\r","");
		}catch(IOException io){
			io.printStackTrace();
			return "quit";
		}
	}

	private static void deleter() {
		System.out.print("specify the node to be removed:");
		String s = getcmd();
		try {
			URI nURI = new java.net.URI(sxeURI.toString().concat(s));
			Client.DELETE(nURI);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (Status e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}			
	}

	private static void poster() {
		java.io.File stepfile;
		java.io.FileInputStream stepstream;
		snobject.snStepProgram program;

		System.out.println("Please specify the STEP file to be uploaded.");
		stepfile = getfilename();
		try {
			//stepfile = new java.io.File(sFile);
			stepstream = new java.io.FileInputStream(stepfile);
			program = new snobject.snStepProgram(stepstream);
		} catch (java.io.IOException ex) {
			System.err.println(ex.getMessage());
			return;
		}

		System.out.println("Attempting POST...");
		try {
			http.Client.POST(sxeURI, program);
		} catch (Exception ex) {
			System.err.println(ex.getMessage());
		}
		System.out.println("Done.");
	}


	// SJJ - SMOD: Convenient for command line posting
	public static void poster(String host, File stepfile, PrintStream writeto) {
		snobject.snStepProgram program;
		try {
			program = new snobject.snStepProgram(new java.io.FileInputStream(stepfile));
		} catch (java.io.IOException ex) {
			writeto.println(ex.getMessage());
			return;
		}

		try{
			sxeURI = new java.net.URI(host + "/snbench/sxe/node/");
		}
		catch (URISyntaxException ex) {
					writeto.println(ex.getMessage());
					return;
		}

		
		snObject resp = null;
		writeto.println("Attempting POST...");
		try {
			resp = http.Client.POST(sxeURI, program);
			writeto.println("Server Response.\n");
			resp.xmlWrite(writeto, 0);
			
		} catch (Exception ex) {
			writeto.println(ex.getMessage());
		}
		writeto.println("Posting Done.");
		
	}
	
	
	private static void showcmds() {
		System.out.println("COMMAND\t\tDoes...");
		System.out.println("post\t\tPosts a STEP file to specifed SXE");
		System.out.println("delete\t\tDeletes a STEP node from specifed SXE");
		System.out.println("quit\t\tquits");
	}

	static public void main(String args[]) {
		if (args.length < 1) {
			usage();
			return;
		}
		br = new BufferedReader(new InputStreamReader(System.in));
		
		// SJJ - SMOD: to pass the STEP file from the command line
		if (args.length==2) {
			poster(args[0], new File(args[1]),System.out);
			return;
		}
		
		try {
			sxeURI = new java.net.URI(args[0] + "/snbench/sxe/node/");
		} catch (java.net.URISyntaxException ex) {
			System.err.println(ex.getMessage());
			return;
		}
		do_stuff();
	}
}
